import java.util.*;

/**
 * @author LKQ
 * @date 2022/4/1 17:52
 * @description 思路：转换为十进制存储，
 */
public class Solution2 {
    public static void main(String[] args) {

    }
    public boolean hasAllCodes(String s, int k) {
        int n = 1 << k;
        if(s.length() < n +  k - 1) {
            return false;
        }
        int num = Integer.parseInt(s.substring(0, k), 2);
        Set<Integer> exists = new HashSet<Integer>();
        exists.add(num);

        for (int i = 1; i + k <= s.length(); ++i) {
            // num(i+1) 和 num(i)之间的关系
            num = (num - ((s.charAt(i - 1) - '0') << (k - 1))) * 2 + (s.charAt(i + k - 1) - '0');
            exists.add(num);
        }
        return exists.size() == (1 << k);

    }
}
